﻿Imports System.Windows.Browser
Imports $YourCompany$.$safeprojectname$_Xap.Utilities

Partial Public Class Page : Inherits UserControl

    Private _callbackID As String
    Private _userName As String

    <ScriptableMember()> _
    Public Property CallbackID() As String   '//allow property to be set from javascript
        Get
            Return _callbackID
        End Get
        Set(ByVal value As String)
            _callbackID = value
        End Set
    End Property

    <ScriptableMember()> _
    Public Property UserName() As String  '//allow property to be set from javascript
        Get
            Return _userName
        End Get
        Set(ByVal value As String)
            _userName = value
        End Set
    End Property

    <ScriptableMember()> _
    Public Event RealNameObtained As EventHandler '//allow javascript to attach to this event

    Private _helloServerSuccessDelegate As ClientAPI.ControlMethodDelegate = Nothing
    Private _onlineStatusSucessDelegate As ClientAPI.ControlMethodDelegate = Nothing
    Private _controlMethodFailDelegate As ClientAPI.ControlMethodDelegate = Nothing

    Public Sub New()
        InitializeComponent()
        HtmlPage.RegisterScriptableObject("PageJS", Me)  '//allow javascript to interact with this object

        '//create event handlers to handle async callbacks
        _helloServerSuccessDelegate = New ClientAPI.ControlMethodDelegate(AddressOf helloServerSuccess)
        _onlineStatusSucessDelegate = New ClientAPI.ControlMethodDelegate(AddressOf onlineStatusSuccess)
        _controlMethodFailDelegate = New ClientAPI.ControlMethodDelegate(AddressOf controlMethodCallbackFail)
    End Sub

    Private Sub btnHello_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) Handles btnHello.Click

        AjaxAnimation.Begin()
        Dim props = New Dictionary(Of String, Object)   'Can we do object initializers for dictionarys in VB?????
        With props
            .Add("Name", UserName)
            .Add("Description", "Some Desc")
        End With

        ClientAPI.InvokeControlMethod(CallbackID, "Hello", props, _helloServerSuccessDelegate, _controlMethodFailDelegate)
    End Sub

    Private Sub btnOnlineStatus_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) Handles btnOnlineStatus.Click
        AjaxAnimation.Begin()
        ClientAPI.InvokeControlMethod(CallbackID, "GetUserInfo", Nothing, _onlineStatusSucessDelegate, _controlMethodFailDelegate)
    End Sub

    Private Sub helloServerSuccess(ByVal payload As Object, ByVal context As Object, ByVal request As Object)
        AjaxAnimation.Stop()
        lblMessage.Text = payload.ToString()
    End Sub

    Private Sub onlineStatusSuccess(ByVal payload As Object, ByVal context As Object, ByVal request As Object)
        AjaxAnimation.Stop()
        Dim user As ScriptObject = CType(payload, ScriptObject)
        Dim userID As Integer = Integer.Parse(user.GetProperty("UserID").ToString())
        If userID > 0 Then '//if user logged in
            '//obtain logged in user name
            UserName = user.GetProperty("DisplayName").ToString()
            lblMessage.Text = String.Format("Hello {0}, I know who you are!", UserName)

            RaiseEvent RealNameObtained(Me, New NameObtainedEventArgs(UserName)) '//if javascript registered event, raise it
        Else
            lblMessage.Text = "You are not logged in.  I cannot speak to you!"
        End If
    End Sub

    Private Sub controlMethodCallbackFail(ByVal payload As Object, ByVal context As Object, ByVal request As Object)
        AjaxAnimation.Stop()
        MessageBox.Show("ERROR: " + payload.ToString())
    End Sub

End Class

